<?php
/**
 * Created by PhpStorm.
 * @User: 王海东
 * @Date: 2018/12/21 13:54
 */

namespace app\adm\controller;

use app\common\model\Admin;
use app\common\model\AdminToken;
use app\common\model\AdminHierarchy;
use app\common\model\Hierarchy;
use app\common\model\User;

use think\facade\Cookie;
use com\Hash;
use think\Db;

class AdminController extends BaseController
{
    /**
     * 登录
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function login()
    {
        $in    = $this->I([
            [
                'name|账号',
                null,
                's',
                'require',
            ],
            [
                'pwd|密码',
                null,
                's',
                'require',
            ],
        ]);

        $a = new Admin();

        $data = $a->getAdminByName($in['name']);

        if(!$data) {
            rj(1,'用户名不存在');
        }

        if(Hash::pwd($in['pwd']) != $data['pwd']) {
            rj(1,'密码输入错误');
        }

        $token = '';
        try {
            $adminToken = new AdminToken();
            $token     = $adminToken->insertToken($data['id']);

            Cookie::set("admToken", $token);
        } catch (\Exception $e) {}

        rjd(['token' => $token]);

    }

    /**
     * 添加管理员
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function add()
    {
        $in    = $this->I([
            [
                'name|账号',
                null,
                's',
                'require',
            ],
            [
                'pwd|密码',
                null,
                's',
                'require',
            ],
            [
                'tel|手机号',
                null,
                's',
                'require|mobile',
            ],
            [
            'hierarchy_id|层级id',
                null,
                'd',
                'require',
            ]
        ]);

        $a = new Admin();

        if($a->checkExits(1,$in['name'])) {
            rj(1,'用户名已经存在');
        }

        if($a->checkExits(2,$in['tel'])) {
            rj(1,'手机号已经存在');
        }

        $h          = new Hierarchy();
        $hierarchy  = $h->infoHierarchy($in['hierarchy_id']);
        if(!$hierarchy) {
            rj(1,'层级不存在');
        }

        $info = $a->info(UID);

        Db::startTrans();

        try {
            $ah         = new AdminHierarchy();
            $admin_id   = $ah->addHierarchy('',$info['admin_id']);

            if(!$admin_id) {
                Db::rollback();
                rj(1,'添加失败');
            }

            $a->add($in['name'],$in['pwd'],$in['tel'],$in['hierarchy_id'],$admin_id);
            Db::commit();
            rj();
        } catch (\Exception $e) {
            Db::rollback();
            rj(1,'添加失败');
        }
    }

    /**
     * 删除管理员
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function delete()
    {
        $in = $this->I([
            [
                'id|管理员编号',
                null,
                'd',
                'require',
            ],
        ]);

        $a      = new Admin();
        $info   = $a->info($in['id']);

        if(!$info) {
            rj(1,'管理员不存在');
        }

        $u = new User();

        $count = $u->getCountByAdmin($in['id']);

        if($count > 0) {
            rj(1,'删除失败！旗下还有用户');
        }

        $ah = new AdminHierarchy();

        $data = $ah->hierarchyList($info['admin_id'],true);

        if($data) {
            rj(1,'删除失败！旗下还有管理员');
        }

        Db::startTrans();

        try {
            if(!$a->delete($in['id'])) {
                Db::rollback();
                rj(1,'删除失败');
            }

            $ah->delHierarchy($info['admin_id']);
            Db::commit();
            rj();
        } catch (\Exception $e) {
            Db::rollback();
            rj(1,'删除失败');
        }
    }

    /**
     * 修改管理员密码
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function editPwd()
    {
        $in    = $this->I([
            [
                'id|管理员编号',
                null,
                's',
                'require'
            ],
            [
                'pwd|密码',
                null,
                's',
                'require'
            ]
        ]);

        $a      = new Admin();
        $info   = $a->info($in['id']);

        if(!$info) {
            rj(1,'管理员不存在');
        }

        if(!$a->editPwd($in['id'],$in['pwd'])) {
            rj(1,'修改失败');
        }

        rj();
    }

    /**
     * 查询管理员列表
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function select()
    {
        $in    = $this->I([
            [
                'name|用户名或手机号',
                null,
                's',
            ],
            [
                'agent_name|代理商名称',
                null,
                's',
            ],
            [
                'start_time|开始时间',
                null,
                's',
            ],
            [
                'end_time|截止时间',
                null,
                's',
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);

        $a = new Admin();

        $where = [
            'name'          => $in['name'],
            'agent_name'    => $in['agent_name'],
            'start_time'    => $in['start_time'],
            'end_time'      => $in['end_time'],
        ];

        $data = $a->select(UID,$in['page'],$in['pageSize'],$where);

        rjd($data);
    }
}